package com.oros.db;

import java.io.Serializable;

import com.vaadin.data.Container.ItemSetChangeEvent;
import com.vaadin.data.Container;

public class AEContainerToContainerListener implements Serializable,
     Container.ItemSetChangeListener,AEListenerClosable {
	/**
	 * 
	 */
	private static final long serialVersionUID = -484573812440710205L;
	final public static int FIRST_LISTENS_SECOND=1;
	final public static int SECOND_LISTENS_FIRST=2;
	final public static int BOTH_LISTEN_EACH_OTHER=FIRST_LISTENS_SECOND | SECOND_LISTENS_FIRST;
         private AETableContainer parent=null;
	public AEContainerToContainerListener(AETableContainer parent)
	{
		this.parent=parent;
	}
	final public static void setContainerToContainerListening(AETableContainer firstContainer,
			AETableContainer secondContainer,int kind)
			{
		        if ((kind & FIRST_LISTENS_SECOND)!=0)
		        {
	               AEContainerToContainerListener listener=new AEContainerToContainerListener(firstContainer); 
	               secondContainer.addListener(listener);
		        }
		        if ((kind & SECOND_LISTENS_FIRST)!=0)
		        {
	               AEContainerToContainerListener listener=new AEContainerToContainerListener(secondContainer); 
	               firstContainer.addListener(listener);
		        }
			}

	@Override
	public boolean isClosed() {
		return parent==null;
	}
	@Override
	public void containerItemSetChange(ItemSetChangeEvent event) {
		if (parent!=null)
		{
			if (parent.opened)
			{
				try
				{
				  parent.refresh();
				}
				catch(Exception e)
				{
					
				}
			}
			else parent=null;
		}		
	}
	
}
